import React, { useReducer } from 'react';

const initState = {
  name: '',
  pwd: '',
  isLoading: false,
  error: '',
  isLoggedIn: false,
};

function loginReducer(state, action) {
  switch (action.type) {
    case 'login':
      return {
        ...state,
        isLoading: true,
        error: '',
      };
    case 'success':
      return {
        ...state,
        isLoggedIn: true,
        isLoading: false,
      };
    case 'error':
      return {
        ...state,
        error: action.payload.error,
        name: '',
        pwd: '',
        isLoading: false,
      };
    default:
      return state;
  }
}

export default { state: initState, reducer: loginReducer };
